#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::guard_init()
{
    guard_process = new QProcess;
    connect(guard_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            this, &MainWindow::guard_finishedCallback);
}

void MainWindow::guard_start()
{
    mplayer->pause();
    this->hide();
    guard_process->start("./Guard");
}

void MainWindow::guard_finishedCallback(int exitCode, QProcess::ExitStatus exitStatus)
{
    this->show();
    mplayer->go_on();
    if(exitStatus != QProcess::NormalExit)
    {
        qCritical() << "The guard process crashed. exitCode: " << exitCode;
    }
    else
    {
        qCritical() << "The guard process exited normally. exitCode: " << exitCode;
    }
}

void MainWindow::guard_errorOccurred(QProcess::ProcessError error)
{
    QMessageBox::critical(this, "error occurred",
                          QString("guard error occurred code: %1")
                          .arg(error));
    qCritical() << "guard error occurred code: " << error;
}
